<template>
  <div class="user-login-dialog">
    <el-form ref="form" label-width="75px" size="mini">
      <el-form-item label="卡号">
        <el-input :disabled="Login.loginType ==1" v-model="Login.name"></el-input>
      </el-form-item>
      <el-form-item label="刷卡数据">
        <el-input type="password" :disabled="Login.loginType ==1" v-model="Login.account"></el-input>
      </el-form-item>
      <el-form-item style="display: flex;justify-content: end">
        <el-button type="primary" @click="LoginBtn(0)">{{ Login.loginType == 1 ? '重刷' : '登录' }}</el-button>
        <el-button @click="Close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { DataManage } from '@/api/DataManageApi'
import SqlTool from '../../utils/SqlTool'
import Utils from '../../utils/Common'
import { AppParams } from '@/utils/appParams'
import { addEventListenerKeydown, MessageSuccess, MessageWarning } from "@/utils";
import SystemApi from "@/api/flow/SystemApi";
import { Emitter } from "@/utils/Emitter";
import { ElLoading } from 'element-plus'
import BaseException from "@/utils/BaseException";

export default {
  name: 'LoginDialog',
  data () {
    return {
      Login: {
        name: null,
        account: null,
        loginType: null
      }
    }
  },
  methods: {
    LoginError: function (_User) {
      Utils.Logout()
      MessageWarning('登录失败')
      Emitter.emit(AppParams.JAVA_SCRIPT_EVENT.USER_LOGIN_FAILURE, _User)
    },
    /**
     * 用户登录
     * @constructor
     */
    LoginBtn (i = 1) {
      const _User = this.Login
      const SQL = SqlTool.GeneratedQuerySql('business_user', null, null,
          `account='${_User.account}'`)
      DataManage.QueryBySql(SQL).then((res) => {
        const US = res.Data

        let LoginUser = {}
        if (Utils.IsEmpty(US.Data)) {
          this.LoginError(_User);
          throw new BaseException("登录失败", 404, _User);
        }
        LoginUser = US.Data[0]
        // if (_User.loginType === 0 && LoginUser.name !== _User.name) {
        //   this.LoginError(_User);
        //   throw new BaseException("登录失败", 401, _User);
        // }

        LoginUser.LoginTime = new Date().getTime()
        Utils.SetLoginUserInfo(LoginUser)
        MessageSuccess('登录成功')
        this.LoadUserPermission(LoginUser)
        this.Close('success')
        Emitter.emit(AppParams.JAVA_SCRIPT_EVENT.USER_LOGIN_SUCCEEDED, LoginUser)
        const loading = ElLoading.service({
          lock: true,
          text: '程序加载中...',
          background: 'rgba(0, 0, 0, 0.7)',
        })
        setTimeout(() => {
          loading.close()
          // location.reload()
        }, 2000)
        Emitter.emit(AppParams.JAVA_SCRIPT_EVENT.USER_LOGIN_COMPLETED, LoginUser)

        if (i === 1 && !Utils.IsAuthentication()) {
          this.OnKey()
        }
      })
    },
    /**
     * 加载用户权限
     * @param UserInfo
     * @constructor
     */
    LoadUserPermission (UserInfo) {
      const SQL = SqlTool.GeneratedQuerySql(SqlTool.TABLES.SYS_USER_PERMISSION, null, null, 'user_id=' + UserInfo.id)
      DataManage.QueryBySql(SQL).then(Perm => {
        if (!Utils.IsEmpty(Perm.Data)) {
          UserInfo.Permissions = Perm.Data
          Utils.SetLoginUserInfo(UserInfo)
        }
      })
      Utils.SetLoginUserInfo(UserInfo)
    },
    Close (res = 'error') {
      this.$emit('close', res)
    },
    OnKey () {
      addEventListenerKeydown((res) => {
        this.Login.account = res
        this.LoginBtn(1)
      }, 100, ['enter'])
    }
  },
  created () {
    const Config = SystemApi.GetSystemConfigInfo()
    if (Config) {
      this.Login.loginType = Config.authentication.LoginType
      if (Config.authentication.LoginType === 1) {
        this.OnKey()
      }
    }
  },
  mounted () {
  }
}
</script>

<style scoped lang="less">
.user-login-dialog {
}
</style>
